package ljye.com.ljye.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;

import ljye.com.ljye.R;
import ljye.com.ljye.utils.DrivingRouteOverlay;
import ljye.com.ljye.utils.Utils;

public class Activity_map extends Activity implements BaiduMap.OnMarkerClickListener, OnGetRoutePlanResultListener {
    protected MapView mMapView;
    protected BaiduMap baiduMap;
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    private double me_latitude;
    private double me_longitude;
    private RoutePlanSearch routePlanSearch;//路径规划
    private boolean isopen;//默认没有开启获取路径
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //百度地图
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_map);

        mMapView = (MapView) findViewById(R.id.map);
        baiduMap = mMapView.getMap();
        //定位我的位置
        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);    //注册监听函数
        initLocation();
        mLocationClient.start();

        //路径规划
        routePlanSearch = RoutePlanSearch.newInstance();
        //显示店铺位置
        init();

        //调整缩放控件的位置
        mMapView.getChildAt(2).setPadding(0,0,0,200);//这是控制缩放控件的位置 px
        // 隐藏百度的LOGO
//        View child = mMapView.getChildAt(1);
        mMapView.getChildAt(1).setVisibility(View.INVISIBLE);

        //隐藏比例尺
        mMapView.getChildAt(3).setVisibility(View.INVISIBLE);
    }
    //点击店铺后弹出
    @Override
    public boolean onMarkerClick(Marker marker) {
        if (me_latitude!=0&&me_longitude!=0&&!isopen) {

            Utils.showToast(Activity_map.this,"导航路线开启 艾迪天才");
            isopen=true;
//            //导航
//            BaiduMapNavigation.openBaiduMapNavi(new NaviParaOption()
//                    .startPoint(new LatLng(me_latitude,me_longitude))
//                    .endPoint(new LatLng(30.511229, 114.410829)), getApplicationContext());

            routePlanSearch.drivingSearch(new DrivingRoutePlanOption()
                    //          驾车路线路况策略                              驾车路线含路况
                    .trafficPolicy(DrivingRoutePlanOption.DrivingTrafficPolicy.ROUTE_PATH_AND_TRAFFIC)
//                设置起点
                    .from(PlanNode.withLocation(new LatLng(me_latitude,me_longitude)))
//               设置终点
                    .to(PlanNode.withLocation(new LatLng(30.511229, 114.410829)))
//                设置驾车路线规划策略              驾乘检索策略常量：最短距离
                    .policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_DIS_FIRST));
            routePlanSearch.setOnGetRoutePlanResultListener(this);
        }

        return false;
    }
    private void init() {
        //1.单例  2.工厂  3.构造器
        MapStatus.Builder builder = new MapStatus.Builder();
        //设置地图中心点  艾迪天才
        builder.target(new LatLng(30.511229,114.410829));
        //设置地图缩放级别 越大越清楚 16 -200米缩放
        builder.zoom(16);
//        //设置地图俯仰角
//        builder.overlook(-45);
//        //设置地图旋转角度，逆时针旋转。
//        builder.rotate(-0);

        MapStatus mapStatus = builder.build();
        //  MapStatusUpdate 描述地图状态将要发生的变化
        // MapStatusUpdateFactory 生成地图状态将要发生的变化
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        baiduMap.setMapStatus(mapStatusUpdate);

        MarkerOptions markerOptions = new MarkerOptions().position(baiduMap.getMapStatus().target)
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.map_icon));
        baiduMap.addOverlay(markerOptions);
        baiduMap.setOnMarkerClickListener(this);

    }

    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span=3000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
        //        用于显示一条驾车路线的overlay
        DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(baiduMap);
//        设置路线数据                              获取所有规划路线方案
        drivingRouteOverlay.setData(drivingRouteResult.getRouteLines().get(0));
        //加入到地图
        drivingRouteOverlay.addToMap();
//        一屏内显示导航
        drivingRouteOverlay.zoomToSpan();

//        地图 Marker 覆盖物点击事件监听函数, 注意根据参数Marker来判断响应某个对象的点击事件
        baiduMap.setOnMarkerClickListener(drivingRouteOverlay);
    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

    }

    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            me_latitude = location.getLatitude();
            me_longitude = location.getLongitude();

            MarkerOptions markerOptions  = new MarkerOptions()
                    .position(new LatLng(me_latitude, me_longitude))
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.my_position));
            baiduMap.addOverlay(markerOptions);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }


}
